/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Assembler;

import java.util.List;
import java.util.ArrayList;
import Common.*;
//import Common.Directive;
//import Common.Function;
//import Common.Instruction;
//import Common.Operator;
//import Common.Utilities;

/**
 *
 * @author Kyle
 */
public class IntermediateData
{
    public String Source_Line;
    public List<String> Errors;
    public int LC;
    public int LineNumber;
    public String Label;
    public Operator Operator;
    public Function Function;
    public Directive Directive;
    public String[] Operands;
    public String Comment;
    public String EquValue;
    public String Absolute;
    public Instruction.Type Type;
    public int ModifyCount;

    public IntermediateData(String source, int lineNum)
    {
        Source_Line = source;
        LineNumber = lineNum;
        ModifyCount = 0;
	Errors = new ArrayList<String>();
    }

    public void PrintData()
    {
        System.out.println(Source_Line);
        System.out.printf("  LC=%03X\n", LC);
        System.out.println("  Label=" + Label);
        if (Directive != null)
        {
            System.out.println("  Directive=" + Directive.Name);
        }
        if (Operator != null)
        {
            System.out.printf("  Operator=%s   Code=%s\n",Operator.Name, Utilities.IntToBinary(Operator.Code, 2));
            
        }
        if (Function != null)
        {
            System.out.printf("  Function=%s   Code=%s\n",Function.Name, Utilities.IntToBinary(Function.Code, 3));
        }
        if (Operands != null)
        {
            for (int i=0; i < Operands.length; i++)
                System.out.printf("  Operand[%d]=%s\n", i, Operands[i]);
        }
        if(Errors.size() > 0)
        {
            //as was limited during class, only 3 errors per line
            for(int i = 0; i < Errors.size(); i++)
            {
                if(i == 3)
                {
                    break;
                }
                System.out.printf("  Error[%d]=%s   %s\n", i, Errors.get(i),Common.Errors.GetErrorMessage(Errors.get(i)));
            }
        }
        System.out.println("  Comment=" + Comment);
        System.out.println("  EquValue=" + EquValue);
        System.out.println("  Relocatable=" + Absolute);
    }

    public void ComputeAbsolute()
    {
        Absolute = "R";
        int index = 0;
        if (Operator != null)
            index = 1;
        if (Directive == Instruction.GetDirective("NOP"))
        {
            Absolute = "A";
            return;
        }

        if ((Operator != null || Directive != null) && Operands != null)
        {
            if (Operands.length > index)
            {
                String temp = Operands[index];
                if (Utilities.ValidateLiteral(temp))
                {
                    temp = temp.substring(1, temp.length());
                }
                if (temp.startsWith("+"))
                {
                    temp = temp.substring(1, temp.length());
                }
                try
                {
                    Integer.parseInt(temp);
                    Absolute = "A";
                }
                catch (NumberFormatException ex)
                {
                    if (Operands[index].contains("=") || Directive == Instruction.GetDirective("EQU") || Directive == Instruction.GetDirective("DAT") || Directive == Instruction.GetDirective("END"))
                    {
                        Absolute = "A";
                    }
                }
            }
            else if (Function != null)
            {
                if (Function == Instruction.GetOperator("CNTL").Functions.get("CLRD") || Function == Instruction.GetOperator("CNTL").Functions.get("CLRT"))
                {
                    Absolute = "A";
                }
            }
            if(Directive == Instruction.GetDirective("EXTRN") || Directive == Instruction.GetDirective("ADCE"))
            {
                Absolute = "M";
            }
        }
        else
        {
            
        }
    }
}